<?php

class SystemConfig extends Controller {

	public function run() {
		$configData = Sdk\lib\Config::GetRoot();
		if (!empty($_POST['import'])) {
			$data = json_decode(base64_decode($_POST['import']), true);
			$configData['base'] = array(
				'witUid' => $data['WitUid'],
				'witSecretId' => $data['WitSecretId'],
				'witSecretKey' => $data['WitSecretKey'],
				'witPid' => $data['WitPid'],
			);
		}
		if (!empty($_POST['db'])) {
			$configData['db'] = array(
				'dsn' => $_POST['db']['dsn'],
				'user' => $_POST['db']['user'],
				'password' => $_POST['db']['password'],
			);
		}
		if (!empty($_POST['redis'])) {
			$configData['redis'] = array(
				'server' => $_POST['redis']['server'],
				'port' => $_POST['redis']['port'],
			);
			if (!empty($_POST['redis']['pconnect'])) {
				$configData['redis']['pconnect'] = 1;
			}
			if (!empty($_POST['redis']['requirepass'])) {
				$configData['redis']['requirepass'] = $_POST['redis']['requirepass'];
			}
		}
		if (!empty($_POST)) {
			Sdk\lib\Config::SetRoot($configData);
		}

		$data = Sdk\lib\Config::GetRoot();
		$this->template('systemConfig', $data);
	}

}